package com.spring.serviceimp.news;

import java.io.File;
import java.util.Calendar;
import java.util.List;
import java.util.Map;


import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.upload.FormFile;
import com.common.Constant;
import com.common.PicUpload;
import com.hibernate.dao.news.ArticlesServiceDao;
import com.hibernate.pojo.Articles;
import com.spring.service.news.ArticlesService;
import com.struts.form.ArticlesForm;

public class ArticlesServiceImp implements ArticlesService
{

	private ArticlesServiceDao articlesServiceDao=null;
	
	public boolean newsAdd(ArticlesForm articlesform)
	{
		Articles articles=new Articles();
		FormFile file = articlesform.getImgUrl();
		String newfilename=null;
		if(file.getFileSize()>0)
		{
			//return folder name
			String datefile=new PicUpload().createFolder(Constant.NEWS_UPLOAD_PIC);
			//database save path
			newfilename=Constant.NEWS_DATABASE_SAVEPATH+datefile+"/";
			//save pic,return pic name
			newfilename+=new PicUpload().picUpload(Constant.NEWS_UPLOAD_PIC+datefile, file);
		}
		try
		{
			BeanUtils.copyProperties(articles,articlesform);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		if(file.getFileSize()>0)
		{
			articles.setImgUrl(newfilename);
		}
		return articlesServiceDao.saveNews(articles);
	}
	
	public boolean updateNewsAudit(ArticlesForm articlesform)
	{
		return articlesServiceDao.updateNewsAudit(articlesform);
	}
	
	
	public Map updateNewsView(Long id)
	{
		return articlesServiceDao.updateNewsView(id);
	}
	
	
	public boolean updateNews(ArticlesForm articlesform)
	{
		FormFile file = articlesform.getImgUrl();
		String newfilename="";
		if(file.getFileSize()>0)
		{
			//return folder name
			String datefile=new PicUpload().createFolder(Constant.NEWS_UPLOAD_PIC);
			//database save path
			newfilename=Constant.NEWS_DATABASE_SAVEPATH+datefile+"/";
			//save pic,return pic name
			newfilename+=new PicUpload().picUpload(Constant.NEWS_UPLOAD_PIC+datefile, file);
		}
		return articlesServiceDao.updateNews(articlesform,newfilename);
	}
	
	public boolean deleteNews(Long id)
	{
		return articlesServiceDao.deleteNews(id);
		
	}
	public Map findNews(int page, int articlesleibieID)
	{
		return articlesServiceDao.findNews(page,articlesleibieID);
	}
	
	public List findNewsTypes(String hql)
	{
		return articlesServiceDao.findNewsTypes(hql);
	}
	
	//batch delete articles
	public boolean deleteBatchNews(String ids)
	{
		return articlesServiceDao.deleteBatchNews(ids);
	}
	
	//batch update articles leibie
	public boolean updateBatchNewsLeibie(String leibies,Long categry)
	{
		return articlesServiceDao.updateBatchNewsLeibie(leibies,categry);
	}
	

	public ArticlesServiceDao getArticlesServiceDao() {
		return articlesServiceDao;
	}

	public void setArticlesServiceDao(ArticlesServiceDao articlesServiceDao) {
		this.articlesServiceDao = articlesServiceDao;
	}

}
